package com.javaear.beangenerator.support.jdbc.mysql;

import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;

/**
 * these javaType-MysqlType standard mapping is view
 * http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-type-conversions.html
 *
 * @author aooer
 */
public enum MysqlStandardTypeDef {

    LONGTEXT(String.class),
    VARCHAR(String.class),
    CHAR(String.class),
    TINYTEXT(String.class),
    TEXT(String.class),
    MEDIUMTEXT(String.class),

    INT(Integer.class),
    SMALLINT(Integer.class),
    MEDIUMINT(Integer.class),
    INTEGER(Integer.class),
    BIGINT(Long.class),

    BIT(Boolean.class),
    TINYINT(Boolean.class),
    BOOL(Boolean.class),
    BOOLEAN(Boolean.class),

    BINARY(byte[].class),
    VARBINARY(byte[].class),
    TINYBLOB(byte[].class),
    BLOB(byte[].class),
    MEDIUMBLOB(byte[].class),
    LONGBLOB(byte[].class),

    FLOAT(Float.class),
    DOUBLE(Double.class),
    DECIMAL(BigDecimal.class),

    DATE(Date.class),
    YEAR(Date.class),
    TIME(Time.class),
    DATETIME(Timestamp.class),
    TIMESTAMP(Timestamp.class),

    ENUM(Character.class),
    SET(Character.class);

    private Class javaType;

    MysqlStandardTypeDef(Class javaType) {
        this.javaType = javaType;
    }

    /**
     * from sqlType Name
     * raise javaType simpleName
     *
     * @param sqlType sqlType
     * @return javaType name
     */
    public static String getJavaTypeName(String sqlType) {
        return MysqlStandardTypeDef.valueOf(sqlType.split("\\(")[0].toUpperCase()).javaType.getSimpleName();
    }
}
